package lib.record;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib.record.recordDao;

/**
 * Servlet implementation class recordServlet
 */
@WebServlet("/recordServlet")
public class recordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public recordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String bno=request.getParameter("bno");
		String uid=(String)request.getSession().getAttribute("uid");
		String flag=request.getParameter("record");
		System.out.println(uid);
		recordDao rd= new recordDao();
		boolean res=false;
		if(flag.equals("1")) {
			res=rd.borrowBook(uid, bno);
			if(res==true) {
				request.setAttribute("b_flag", 11);
			}
			else {
				request.setAttribute("b_flag", 10);
			}
		}
		else {
			res=rd.returnBook(uid, bno);
			if(res==true) {
				request.setAttribute("b_flag", 1);
			}
			else {
				request.setAttribute("b_flag", 0);
			}
		}
		request.getRequestDispatcher("borrow_result.jsp").forward(request, response);		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
